package edu.geo.filters;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.geo.containers.GeoJson;
import edu.geo.containers.Json;
import edu.geo.protocol.Properties;
import edu.geo.containers.geom.impl.Point;
import edu.geo.containers.geomdomain.Grid;
import edu.geo.exception.GeoInfoException;
import edu.geo.protocol.impl.GeomDomainAddResponse;
import edu.geo.service.IGeometricDomainServiceFacade;

/*
 * Ejemplo de petición e borrado de un dominio geométrico 
{
    "type": "Operation",
    "name": "retrieveGridDomain",
    "properties": {
        "user": "geoinfouser",
        "domainId": "10"
    }
}
*/
public class GridGeomDomainProcessor extends GeoProcessDecorator{

	private static final Log LOGGER = LogFactory
			.getLog(GeometricDomainAddProcessor.class);
	
	public static final String OPERATION_NAME = "deleteGeometricDomainResult";
	public static final String OPERATION_RESULT_SUCCESSFULLY = "successfully";
	public static final String OPERATION_RESULT_DAMAGED = "damaged";

	private IGeometricDomainServiceFacade geomDomainService;
	
	public GridGeomDomainProcessor(IGeometricDomainServiceFacade geomDomainService) {
		super();
		this.geomDomainService = geomDomainService;
	}

	public Json dispatchResult(Json requestParams) {
		Json result = null;
		String domaindId=requestParams.getProperties().getDomainId();
		int domain=Integer.parseInt(domaindId);
		try {
			Grid grid = geomDomainService.retrieveGridGeomDomain(domain);
			grid.getId();
			grid.getIntSize();
			grid.getNumPointsX();
			grid.getNumPointsY();
			grid.getOrigin();
			List<Point> points = grid.getGridPoints();
			result = new Json();
			result.setType("FeatureCollection");
			result.setName("ResultDomainGrid");
			List<Json> features = new ArrayList<Json>();
			for(Point point:points)
            {
					Json feature = new Json();
					feature.setType("Feature");
					GeoJson geometry = new GeoJson();
					geometry.setType("Point");
					ArrayList<Double> coordinates = new ArrayList<Double>();
					coordinates.add((double) point.getX());
					coordinates.add((double) point.getY());
					geometry.setCoordinates(coordinates);
					feature.setGeometry(geometry);
					features.add(feature);
				
			}
			result.setFeatures(features);
		} catch (GeoInfoException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e.getMessage(),e);
		}
		LOGGER.info("grid domaina process!! domain id "+domaindId);
		return result;
	}
}
